<?php

namespace App\Http\Controllers\Admin;

use App\Customer;
use App\Task;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CustomerController extends Controller
{
    function index()
    {
        $customers = Customer::all();
        //return $customers;
       return view('admin.customer.index')->with('customers',$customers);
    }
    function store(Request $request)
    {
        //return$request->all();
        Customer::create($request->all());
        return redirect(route('admin.customer.index'))->with('success', '新增客户成功~');
    }
    function create()
    {
        return view('admin.customer.create');
    }
    function edit($id)
    {
        $customers = Customer::find($id);
        return view('admin.customer.edit')->with('customers',$customers);
    }
    function update(Request $request,$id)
    {
        $customers = Customer::find($id);
        $customers->update($request->all());
        return redirect(route('admin.customer.index'))->with('success', '编辑客户成功~');
    }
    function show($id)
    {
        $customers = Customer::find($id);
        //return $customers;
        $task_no =Task::where('ok',true)->where('customer_id',$id)->get();
        $task_yes =Task::where('ok',false)->where('customer_id',$id)->get();
        //return $task_no;
        return view('admin.customer.show')->with('customers',$customers)->with('task_no',$task_no)->with('task_yes',$task_yes);
    }
    function add_customer(Request $request)
    {
        //return($request->data);
        //return($request->all());
        Customer::create($request->all());
    }
    function add_task(Request $request)
    {
        //return($request->data);
        //return($request->all());
        Task::create($request->all());
    }
    function edit_task(Request $request)
    {
        //return $request->all();
        $task = Task::find($request->id);
        //return $task;
        $task->update($request->all());

    }
    function ok(Request $request)
    {
        //return($request->data);
        //return($request->all());
        //return $id;
        $id = $request->id;
        //return $id;
        $task = Task::find($id);
        //return $task;
        $task->update($request->all());

    }
    function upload(Request $request)
    {
        if ($request->hasFile('Filedata') and $request->file('Filedata')->isValid()) {

            $result = array();

            //文件类型
            $allow = array('image/jpeg', 'image/png', 'image/gif');

            $mine = $request->file('Filedata')->getMimeType();
            if (!in_array($mine, $allow)) {
                $result['status'] = 0;
                $result['info'] = '文件类型错误，只能上传图片';
                return $result;
            }

            //文件大小判断$filePath
            $max_size = 1024 * 1024 * 3;
            $size = $request->file('Filedata')->getClientSize();
            if ($size > $max_size) {
                $result['status'] = 0;
                $result['info'] = '文件大小不能超过3M';
                return $result;
            }

            //上传文件夹，如果不存在，建立文件夹
            $date = date("Y_m");
            $path = getcwd() . '/finder/images/' . $date;
            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }

            //生成新文件名
            $extension = $request->file('Filedata')->getClientOriginalExtension();      //取得之前文件的扩展名

            $file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $extension;
            $request->file('Filedata')->move($path, $file_name);

            //返回新文件名
            $result['status'] = 1;
            $result['info'] = '/finder/images/' . $date . '/' . $file_name;

//            $filePath = getcwd() . '/finder/images/' . $date . '/' . $file_name;
//            uptoQiniu($filePath);
//            $result['info'] = config('xSystem.qiniu') . $file_name;
            return $result;
        }
    }
}
